<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="名称" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <div class="w-60">
                        <el-select v-model="queryParams.status" clearable placeholder="请选择状态" style="width: 100%;">
                            <el-option label="全部" :value="2" />
                            <el-option label="正常" :value="1" />
                            <el-option label="禁用" :value="0" />
                        </el-select>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['hotel_basic/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="名称" prop="name" show-overflow-tooltip align="center"/>
                    <el-table-column label="分类" prop="cid" show-overflow-tooltip align="center">
                        <template #default="{ row }">
                            {{ cateLists.find(item => item.id == row.cid)?.name }}
                        </template>
                    </el-table-column>
                    <el-table-column label="类型" prop="type" show-overflow-tooltip align="center">
                        <template #default="{ row }">
                            {{ row.type == 1 ? '普通' : row.type == 2 ? '豪华' : '尊贵' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="类型" prop="file_type" show-overflow-tooltip align="center">
                        <template #default="{ row }">
                            {{ row.file_type == 1 ? '礼物' : '音乐' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="图片/音乐" prop="file_url" show-overflow-tooltip align="center">
                        <template #default="{ row }" >
                            <div style="display: flex;align-items: center;justify-content: center;">
                                <el-image :src="row.file_url" style="width: 100px; height: 100px;" :fit="row.file_url.includes('http') ? 'cover' : 'contain'" v-if="row.file_type == 1"/>
                                <audio :src="row.file_url" controls style="width: 100px;" v-else-if="row.file_type == 2"></audio>
                            </div>
                            
                        </template>
                    </el-table-column>
                    <el-table-column label="排序" prop="sort" show-overflow-tooltip align="center"/>
                    <el-table-column label="状态" prop="status" show-overflow-tooltip align="center">
                        <template #default="{ row }">
                            <el-tag v-if="row.status == 1" type="success">正常</el-tag>
                            <el-tag v-else type="danger">禁用</el-tag>
                        </template>
                    </el-table-column>
                    

                    <el-table-column label="操作" width="120" fixed="right" align="center">
                        <template #default="{ row }">
                             <el-button
                                v-perms="['hotel_basic/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button v-if="row.status == 1"
                                v-perms="['hotel_basic/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                            <el-button v-if="row.status == 0"
                                v-perms="['hotel_basic/recover']"
                                type="primary"
                                link
                                @click="handleRecover(row.id)"
                            >
                                恢复
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>
        
<script lang="ts" setup name="hotelBasicLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { sacrificeLists, sacrificeDelete, sacrificeRecover, sacrificeCateLists } from '@/api/sacrificeManage'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { shallowRef, ref, reactive, nextTick } from 'vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    name: '',
    status: 2 as number | string
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: sacrificeLists,
    params: {
        name: queryParams.name,
        status: queryParams.status == 2 ? '' : queryParams.status
    }
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await sacrificeDelete({ id })
    getLists()
}

// 恢复
const handleRecover = async (id: number) => {
    await feedback.confirm('确定要恢复？')
    await sacrificeRecover({ id })
    getLists()
}

const cateLists = ref<any[]>([])
const getCateLists = async () => {
    const res = await sacrificeCateLists()
    cateLists.value = res.lists
}
getCateLists()
getLists()
</script>

